mod user;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;

//========SQL==========//
//========Table for users SQL==========//
const USER_QUERY_NO_PWD_BY_EMAIL_SQL: &'static str = "SELECT id, ws_id, fullname, email, created_at FROM users WHERE email = $1";
const USER_QUERY_BY_EMAIL_SQL: &'static str = "SELECT id, ws_id, fullname, password_hash, email, created_at FROM users WHERE email = $1";
const USER_INSERT_SQL: &'static str = r#"INSERT INTO users(email, fullname, password_hash)
                                                           VALUES ($1, $2, $3)
                                                           RETURNING *"#;



#[derive(Debug, Clone, FromRow, Serialize, Deserialize, PartialEq)]
pub struct User {
    pub id: i64,
    pub ws_id: i64,
    pub fullname: String,
    #[sqlx(default)]
    #[serde(skip)]
    pub password_hash: Option<String>,
    pub email: String,
    pub created_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize, FromRow, PartialEq)]
pub struct ChatUser {
    pub id: i64,
    pub fullname: String,
    pub email: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CreateUser {
    pub fullname: String,
    pub email: String,
    pub password: String,
    pub workspace: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum ChatType {
    Single(String),
    Group(String),
    Private(String),
    PublicChannel(String),
}

#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
pub struct Chats {
    id: usize,
    name: String,
    chat_type: ChatType,
    members: Vec<usize>,
    created_at: DateTime<Utc>,
}

#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
pub struct Messages {
    id: usize,
    chat_id: usize,
    sender_id: usize,
    content: String,
    files: Vec<String>,
    created_at: DateTime<Utc>,
}

impl User {
    pub fn new(id: i64, fullname: &str, email: &str) -> Self {
        Self {
            id: id,
            ws_id: 0,
            fullname: fullname.to_string(),
            password_hash: None,
            email: email.to_string(),
            created_at: Utc::now(),
        }
    }
}